package com.saui.traceu.controller;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;

public class LocationHelper {

	private static LocationHelper lh;
	private LocationListener locationListener;
	LocationManager locationManager; // the service object (thing) that gets one locations
	private String provider;
	boolean isPaused;

	// Constants defining the parameters used with location services
	final int MIN_TIME = 5*1000; // minimum time in milliseconds between location updates (or something to that effect)
	final int MIN_DISTANCE = 1; // minimum distance you have to move before something occurs with location

	// private variables needed for using location service
	String serviceString; // the location service that will be used 

	/**
	 * 
	 * @param lm a LocationManager, since it's easier to get one from an Activity
	 * @param ll a locationListener, since it's easier for the calling activity to make one.
	 */
	private LocationHelper(LocationListener ll, LocationManager lm) {
		isPaused = true;
		locationManager = lm;
		this.locationListener = ll;
		provider = getLocationProvider();
	}

	/**
	 * Get an instance of a LocationManager object
	 * 
	 * @param lm a location manager object (so far as I can tell, it's easiest for Activities to make these.
	 * @param a location listener. Again, it's better to let the calling Activity handle making this.
	 * @return the singleton instance of the LocationHelper
	 */
	public static LocationHelper getLocationHelper(LocationListener ll, LocationManager lm) {
		if (lh == null) {
			lh = new LocationHelper(ll,lm);
		}
		return lh;
	}

	/**
	 * 
	 * @return the most up to date location reading.
	 */
	public Location getLatestLocation() {
		if (this.locationManager == null) {
			return null;
		}
		Location l = locationManager.getLastKnownLocation(provider);
		return l;
	}

	/**
	 * Handles restarting a request for locations.
	 */
	public String startLocationUpdates() {
		if (isPaused) {
			isPaused = false;
			LocationManager lm= locationManager; 
			String pv = provider;
			LocationListener ll = locationListener;
			
			if (lm == null) 
				return"null manager";
			if (pv == null)
				return "null provider";
			if (ll == null)
				return "null listener";
			locationManager.requestLocationUpdates(provider, MIN_TIME, MIN_DISTANCE, locationListener);
			return pv;
		}return "not paused";
	}

	/**
	 * stop listening for updates.
	 */
	public void pauseLocationUpdates() {
		isPaused = true;
		locationManager.removeUpdates(locationListener);
	}

	/**
	 * This method exists to simplify getting a Location provider to a single method call.
	 * Until we want to do something more complicated, this is easiest.
	 * 
	 * @return a location provider based on default criteria.
	 */
	private String getLocationProvider() {
		//Criteria for location
		if (this.locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			return LocationManager.GPS_PROVIDER;
		}
		else if (this.locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
			return LocationManager.NETWORK_PROVIDER; // TODO: improve code so that provider is updated.
		} else {
			return LocationManager.PASSIVE_PROVIDER;
		}
	}	
}